package com.qf.team2.netdisc.admin.interceptor;

import com.qf.team2.netdisc.common.exception.BusinessException;
import io.swagger.models.auth.In;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 管理员拦截器
 *
 * @author Simon_Wong
 * @date 2021/05/21
 * @since 2021/5/20 20:41
 */
@SuppressWarnings("AlibabaLowerCamelCaseVariableNaming")
public class AdminInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request
            , HttpServletResponse response, Object handler) throws BusinessException {
        System.out.println("管理员拦截器已生效");
        StringBuffer requestURL = request.getRequestURL();
        if (requestURL.toString().contains("login")){
            System.out.println("白名单：login");
            return true;
        }
        Cookie[] cookies = request.getCookies();
        if (cookies == null||cookies.length==0) {
            throw new BusinessException("未登录");
        }
        Integer character = null;
        for (Cookie cookie : cookies) {
            if ("character".equals(cookie.getName())) {
                character = Integer.valueOf(cookie.getValue());
            }
        }
        if (character == null||character!=0) {
            throw new BusinessException("没有权限");
        }
        System.out.println("通过管理员拦截器");
        return true;
    }
}
